global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A24_mp_confusion.log, replace name(tabl)

capture noi {  

    * v5
    * table_mp_confusion

    texdoc init ${tab_dir}/appendix/Table_A24_mp_confusion.tex, replace
    texdoc stlog, nolog
    * we only use the merged dataset here to avoid issues with with the proprietary patstat data
    use ${final_dir}/MP_DHOZ_common_data_docdb.dta, clear

    * Summary stats on docdb level
    cap log using ${numb_dir}/Table_A24_numbers.log, replace name(numb)
    duplicates drop
    duplicates report docdb_family_id 
    cap log close numb

    preserve
    duplicates drop docdb_family_id, force
    foreach autovers in auto90 auto95 pauto90 { 
        count 
        sleep 1000
        texdoc local tot `r(N)'
        sleep 1000
        texdoc local tot_p : display string((`tot' / `tot') * 100, "%9.1f")

        count if automat_puettmann == 1 
        sleep 1000
        texdoc local MPa `r(N)'
        di `MPa'
        sleep 1000
        di `tot'
        sleep 1000
        texdoc local MPa_p : display string((`MPa' / `tot') * 100, "%9.1f")
        di `MPa_p'
        count if automat_puettmann == 0
        sleep 1000
        texdoc local MPna `r(N)'
        sleep 1000
        texdoc local MPna_p : display string((`MPna' / `tot') * 100, "%9.1f")

        sleep 1000
        count if `autovers' == 1 
        sleep 1000
        texdoc local `autovers'a `r(N)'
        sleep 1000
        texdoc local `autovers'a_p : display string((``autovers'a' / `tot') * 100, "%9.1f")
        count if `autovers' == 0
        sleep 1000
        texdoc local `autovers'na `r(N)'
        sleep 1000
        texdoc local `autovers'na_p : display string((``autovers'na' / `tot') * 100, "%9.1f")

        sleep 1000
        count if automat_puettmann == 1 & `autovers' == 1
        sleep 1000
        texdoc local MPa_`autovers'a `r(N)'
        sleep 1000
        texdoc local MPa_`autovers'a_p : display string((`MPa_`autovers'a' / `tot') * 100, "%9.1f")
        count if automat_puettmann == 0 & `autovers' == 1
        sleep 1000
        texdoc local MPna_`autovers'a `r(N)'
        sleep 1000
        texdoc local MPna_`autovers'a_p : display string((`MPna_`autovers'a' / `tot') * 100, "%9.1f")
        count if automat_puettmann == 1 & `autovers' == 0
        sleep 1000
        texdoc local MPa_`autovers'na `r(N)'
        sleep 1000
        texdoc local MPa_`autovers'na_p : display string((`MPa_`autovers'na' / `tot') * 100, "%9.1f")
        count if automat_puettmann == 0 & `autovers' == 0
        sleep 1000
        texdoc local MPna_`autovers'na `r(N)'
        sleep 1000
        texdoc local MPna_`autovers'na_p : display string((`MPna_`autovers'na' / `tot') * 100, "%9.1f")
        sleep 8000
    }

    restore
    * output table (via texdoc)
    texdoc local notes "This table reports the shares of machinery patents that we (auto95) or Mann and Puettmann classify as automation. The sample is the set of US patents analyzed by Mann and Puettmann."
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex

    \begin{tabular}{@{}lr*{3}{S[table-format = 2.1]}}
    \toprule
    \multirow{2}{*}{\makecell[l]{Machinery \\ patents}} & & \multicolumn{2}{c}{\makecell{MP\\ Automation}}  & \mc{\makecell{Total \\ (\si{\percent})}} \\
    \cmidrule{3-4}
    & & \mc{Yes (\si{\percent})} & \mc{No (\si{\percent})}  & \\
    \midrule
    \multirow{3}{*}{\makecell[l]{DHOZ \\ Automation}} & Yes (\si{\percent})    & `MPa_auto95a_p'& `MPna_auto95a_p'& `auto95a_p'\\
    & No  (\si{\percent})     & `MPa_auto95na_p' & `MPna_auto95na_p'& `auto95na_p'\\
    & Total  (\si{\percent})   & `MPa_p' & `MPna_p'& `tot_p'\\
    \bottomrule
    \end{tabular}

    tex*/

    tex `texpostamble'
    texdoc close


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}